package com.winhxd.b2c.message.utils;



import com.winhxd.b2c.message.sms.common.SmsConstant;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.URI;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;

public class HttpClientUtil {
    
    private HttpClientUtil(){
        // 构造私有
    }

    /**
     * 发起HttpClient请求(get方式)
     * @param url
     * @param relative
     * @param pair 其他参数
     * @return string 执行结果
     * */
    public static String getSend(String url,String relative,NameValuePair[] pair) throws IOException {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod();
        String rs = "";
        InputStream in ;
        client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
        client.getHttpConnectionManager().getParams().setSoTimeout(3000);
        URI base = new URI(url, false);
        method.setURI(new URI(base,relative, false));
        method.setQueryString(pair);
        int result = client.executeMethod(method);
        if (result == HttpStatus.SC_OK) {
            in = method.getResponseBodyAsStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
            byte[] buffer = new byte[1024];
            int len ;
            while ((len = in.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            rs = URLDecoder.decode(baos.toString(), SmsConstant.URLDECODER_UTF8);
        }
        method.releaseConnection();
        return rs;
    }
    
    /**
     * 发起HttpClient请求(post方式)
     * @param url
     * @param relative
     * @param pair 其他参数
     * @return string 执行结果
     * */
    public static String postSend(String url,String relative,NameValuePair[] pair) throws IOException {
        return postSend(url,relative,pair,SmsConstant.URLDECODER_UTF8,SmsConstant.URLDECODER_UTF8);
    }

    /**
     * 发起HttpClient请求(post方式)
     * @param url
     * @param relative
     * @param pair 其他参数
     * @param urldecoder 编码
     * @return string 执行结果
     * */
    public static String postSend(String url,String relative,NameValuePair[] pair,String methoduUnicode,String urldecoder) throws IOException {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod();
        String rs = "";
        InputStream in ;
        client.getHttpConnectionManager().getParams().setConnectionTimeout(3000);
        client.getHttpConnectionManager().getParams().setSoTimeout(3000);
        URI base = new URI(url, false);
        method.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset="+methoduUnicode);
        method.setURI(new URI(base,relative, false));
        method.setRequestBody(pair);
        int result = client.executeMethod(method);
        if (result == HttpStatus.SC_OK) {
            in = method.getResponseBodyAsStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len ;
            while ((len = in.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            rs = URLDecoder.decode(baos.toString(), urldecoder);
        }
        method.releaseConnection();
        return rs;
    }
}
